
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using ZuDoKu;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace ZuDoKu.Controls
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public partial class Label : Control
    {
        private string _text;
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        private SpriteFont _font = MainGame.Instance.DefaultFont;
        public SpriteFont Font
        {
            get { return _font; }
            set { _font = value; }
        }

        public override int Width
        {
            get
            {
                return (string.IsNullOrEmpty(Text) || base.Width > 0 ? base.Width :
                    (int)_font.MeasureString(Text).X);
            }
            set
            {
                base.Width = value;
            }
        }
        public override int Height
        {
            get
            {
                return (string.IsNullOrEmpty(Text) || base.Height > 0 ? base.Height :
                    (int)_font.MeasureString(Text).Y);
            }
            set
            {
                base.Height = value;
            }
        }

        private Color _color = Color.White;
        public Color Color
        {
            get { return _color; }
            set { _color = value; }
        }

        public Label()
        {
            _font = MainGame.Instance.DefaultFont;
            AcceptsInput = false;
        }

        public override void Draw(SpriteBatch batch, GameTime gameTime, int offsetX, int offsetY, byte transparency)
        {
            byte inheritedTransparency = (byte)MathHelper.Clamp((float)transparency - (float)(255 - Transparency), 0, 255);

            if (!string.IsNullOrEmpty(Text) && Font!=null)
            {
                Vector2 destination = new Vector2(offsetX + X, offsetY + Y);
                Color c = new Color(Color.R, Color.G, Color.B, (byte)Math.Min(Color.A, inheritedTransparency));
                batch.DrawString(
                    Font,
                    Text,
                    destination,
                    c,
                    Rotation,
                    Origin,
                    Scale,
                    SpriteEffects.None,
                    Z);
            }

            base.Draw(batch, gameTime, offsetX, offsetY, transparency);
        }

        public override string ToString()
        {
            return _text ?? GetType().FullName;
        }
    }
}


